home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: Reading stdin twice
- Date: Thu, 04 Apr 96 18:34:40 GMT
- Organization: none
- Message-ID: <828642880snz@genesis.demon.co.uk>
- References: <4jsojd$1hlk@news.missouri.edu> <31628AEC.ABD@wight.hursley.ibm.com>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <31628AEC.ABD@wight.hursley.ibm.com>
- dwater@hursley.ibm.com "Max Waterman" writes:
-
- >Try :
- >
- >rewind( stdin )
- >
- >in between the functions.
-
- This won't work if the file doesn't support seeking e.g. it is connected
- to a device such as a keyboard which is quite likely with stdin.
-
- >Max.
- >
- >James P. Cooper wrote:
- >>
- >> Hi,
- >>
- >> I'm writing a program, and I'd like two functions to read the stdin
- >> stream. Is this possible? I realize I could pass a pointer to the
- >> buffer after the first function fread's stdin, but this would break
- >> something else more serious.
- >>
- >> Example of what I'm doing:
- >>
- >> void read_first() {
- >> char *input;
- >> FILE *infile;
- >>
- >> length = 5000;
- >> fread(input, sizeof(char), length, stdin);
- >>
- >> /* Do stuff with data here... */
- >> }
- >>
- >> void read_again() {
- >> char *input;
- >> FILE *infile;
- >>
- >> length=5000;
- >> fread(input,sizeof(char),length,stdin);
- >>
- >> /* Do something different */
- >> }
- >>
- >> I've tried to use fwrite to rewrite the buffer back to stdin, but that
- >> didn't work (presumably because stdin is read-only).
- >>
- >> Any ideas?
-
- What you need is a routine that reads a buffer and then passes that buffer
- to each function in turn. If you have to scan the input data twice in sequence
- then:
-
- 1. you can do it directly with rewind(stdin) if the input file supports
- seeking
-
- 2. You could store the contents of the entire input in memory as it is
- being scanned by the first funciton. The second function just scans the
- memory image.
-
- 3. You could copy the input out to a temporary file as it is being scanned
- by the first function. The second function rescans the temporary file.
- tmpfile() might be useful here.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-